package cn.abyss.admin.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2020/12/15
 */
public class BaseLogger {
    private static final Logger logger = LoggerFactory.getLogger(BaseLogger.class);

    public BaseLogger() {
    }

    public static void trace(String msg) {
        logger().trace(msg);
    }

    public static void trace(String msg, Throwable e) {
        logger().trace(msg, e);
    }

    public static void trace(String format, Object arg) {
        logger().trace(format, arg);
    }

    public static void debug(String msg) {
        logger().debug(msg);
    }

    public static void debug(String msg, Throwable e) {
        logger().debug(msg, e);
    }

    public static void debug(String format, Object arg) {
        logger().debug(format, arg);
    }

    public static void debug(String format, Object... args) {
        logger().debug(format, args);
    }

    public static void info(String msg) {
        logger().info(msg);
    }

    public static void info(String msg, Throwable e) {
        logger().info(msg, e);
    }

    public static void info(String format, Object arg) {
        logger().info(format, arg);
    }

    public static void info(String format, Object... args) {
        logger().info(format, args);
    }

    public static void warn(String msg) {
        warn(msg, (Throwable)null);
    }

    public static void warn(String msg, Throwable e) {
        logger().warn(msg, e);
    }

    public static void warn(String format, Object arg) {
        logger().warn(format, arg);
    }

    public static void error(String msg) {
        logger().error(msg);
    }

    public static void error(String msg, Throwable e) {
        logger().error(msg, e);
    }

    public static void error(String format, Object arg) {
        logger().error(format, arg);
    }

    public static void error(String format, Object... args) {
        logger().error(format, args);
    }

    private static StackTraceElement findCaller() {
        StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
        if (null == callStack) {
            return null;
        } else {
            StackTraceElement caller = null;
            String thisClassName = BaseLogger.class.getName();
            boolean isFoundThisClass = false;
            StackTraceElement[] var4 = callStack;
            int var5 = callStack.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                StackTraceElement s = var4[var6];
                if (thisClassName.equals(s.getClassName())) {
                    isFoundThisClass = true;
                } else if (isFoundThisClass && !thisClassName.equals(s.getClassName())) {
                    caller = s;
                    break;
                }
            }

            return caller;
        }
    }

    private static Logger logger() {
        StackTraceElement caller = findCaller();
        if (caller == null) {
            return logger;
        } else {
            logger.debug("Logging from: {}!{}(), line: {}", new Object[]{caller.getClassName(), caller.getMethodName(), caller.getLineNumber()});
            return LoggerFactory.getLogger(caller.getClassName());
        }
    }
}

